package xin.mydreams.mybill.widget.user.service.impl;

import java.util.UUID;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import xin.mydreams.mybill.util.UserUtil;
import xin.mydreams.mybill.widget.user.entity.User;
import xin.mydreams.mybill.widget.user.entity.enums.CheckRole;
import xin.mydreams.mybill.widget.user.entity.enums.Status;
import xin.mydreams.mybill.widget.user.service.IUserService;

/**
 * 系统内置帐初始化.<br>
 * 
 * 
 */
@Component
public class InnerUserInitializer {
	@Autowired
	private IUserService userService;

	@PostConstruct
	public void initialize() throws Exception {
		String[] userNames=new String[]{"guzehao","liudan","yanchangjiang","lulifeng"};
		for (String userName: userNames ) {
			saveUser(userName);
		}
	}
	public void saveUser(String userName){
		String salt = UUID.randomUUID().toString();
		String password = UserUtil.getEncryptPsw("123456a", salt);
		User user = userService.findByUserName(userName);
		if (user == null) {
			user = new User();
			user.setUserName(userName);
			user.setSalt(salt);
			user.setPassword(password);

			user.setDisplayName(userName);
			user.setPhotoUrl("");
			user.setExpireAt("0");
			user.setStatus(Status.normal);
			user.setCheckRole(CheckRole.SUPER_ADMIN);
			userService.save(user);
		}
	}
}
